/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.ui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import net.worlds.WorldsException;
import net.worlds.Main;
import net.worlds.lua.Event;
import net.worlds.object.GameObject;
import net.worlds.object.HiddenScript;
import net.worlds.object.Part;
import net.worlds.object.Script;


public class TreePopupMenu extends JPopupMenu{
    JMenuItem copyItem;
    JMenuItem cutItem;
    JMenuItem pasteItem;
    JMenu eventsMenu;
    //ArrayList<JMenuItem> eventsItems;
    public TreePopupMenu(final GameObject obj)
    {
        try {
            UI.clearSelection(false);
            UI.select(obj);
            UI.updateSelection();
        } catch (WorldsException ex) {
            Main.error(ex);
        }
        copyItem = new JMenuItem("Copy");
        if(!UI.canCopy())
            copyItem.setEnabled(false);
        copyItem.addActionListener(new ActionListener()
            {

                public void actionPerformed(ActionEvent e) {
                    UI.doCopy();
                }

            });
        add(copyItem);
        cutItem = new JMenuItem("Cut");
        if(!UI.canCopy())
            cutItem.setEnabled(false);
        cutItem.addActionListener(new ActionListener()
            {

                public void actionPerformed(ActionEvent e) {
                    try {
                        UI.doCopy();
                        UI.deleteSelection();
                    } catch (WorldsException ex) {
                        Main.error(ex);
                    }
                }

            });
        add(cutItem);
        pasteItem = new JMenuItem("Paste Into");
        if(!UI.canPaste())
            pasteItem.setEnabled(false);
        pasteItem.addActionListener(new ActionListener()
            {

                public void actionPerformed(ActionEvent e) {
                    try {
                        UI.doPaste(true);
                    } catch (WorldsException ex) {
                        Main.error(ex);
                    }
                }

            });
        add(pasteItem);
        if(obj instanceof Script)
        {
            JMenuItem runScriptItem = new JMenuItem("Run Script");
            runScriptItem.addActionListener(new ActionListener()
            {

                public void actionPerformed(ActionEvent e) {
                    try {
                        ((Script) obj).run();
                    } catch (WorldsException ex) {
                        Main.error(ex);
                    }
                }

            });
            add(runScriptItem);
            JMenuItem editScriptItem = new JMenuItem("Edit Script");
            editScriptItem.addActionListener(new ActionListener()
            {

                public void actionPerformed(ActionEvent e) {
                    try {
                        UI.editScript((Script) obj);
                    } catch (WorldsException ex) {
                        Main.error(ex);
                    }
                }

            });
            add(editScriptItem);
        }
        else if(obj instanceof Part)
        {
            JMenuItem setCharacterItem = new JMenuItem("Set Default Character...");
            setCharacterItem.addActionListener(new ActionListener()
            {

                public void actionPerformed(ActionEvent e) {
                        Main.runInMainThread(new Runnable()
                        {
                            public void run() {
                                try {
                                    obj.world.setDefaultCharacter((Part) obj);
                                } catch (WorldsException ex) {
                                    Main.error(ex);
                                }
                            }
                        });
                }
                
            });
            add(setCharacterItem);
        }
        eventsMenu = new JMenu("Events");
        add(eventsMenu);
        for(final Event e : obj.getEvents())
        {
            JMenuItem m = new JMenuItem(e.eventName);
            final String scriptName = obj.toString()+"."+e.eventName;
            try {
                if (obj.hasChild(scriptName) && obj.getChild(scriptName).getStringProp("Source").trim().split("\n").length > 1) {
                    m.setForeground(Color.blue);
                }
            } catch (WorldsException ex) {
                ex.printStackTrace();
            }
            m.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent evt) {
                    try {
                        if (obj.hasChild(scriptName) && !(obj.getChild(scriptName) instanceof HiddenScript)) {
                            obj.getChild(scriptName).remove();
                        }
                        if (!obj.hasChild(scriptName)) {
                            HiddenScript script = (HiddenScript) obj.world.createObject("HiddenScript");
                            String arguments = Event.getArguments(obj.getClass(), e.eventName);
                            String[] parts = arguments.split(", ");
                            StringBuilder builder = new StringBuilder();
                            int i = 0;
                            for(String arg : parts)
                            {
                                String[] subparts = arg.split(" ");
                                builder.append(subparts[0]);
                                if(i != parts.length - 1)
                                    builder.append(",");
                                i++;
                            }
                            String argString = builder.toString();
                            script.setProp("Name", scriptName, true);
                            script.setProp("AutoPrepend", "link(script.Parent."+e.eventName+", function("+argString+") ", true);
                            script.setProp("AutoAppend", "\nend)", true);
                            script.setProp("Source", "--Parameters you can use: "+arguments, true);
                            script.setParent(obj);
                        }
                        UI.editScript((Script) obj.getChild(scriptName));
                    } catch (WorldsException ex) {
                        ex.printStackTrace();
                    }
                }
            });
            eventsMenu.add(m);
        }
    }
}
